package com.dhcc.sdc.cdss.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sdc.cdss.dao.SdcCdssItemLogMapper;
import com.dhcc.sdc.cdss.dao.SdcCdssItemMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssItemEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssItemLogEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssItemLogService;
import com.dhcc.sds.base.dao.SdsQcentityMapper;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 80572
 * @version 1.0
 * @date 2023/2/1 10:15
 */
@Service("sdcCdssItemLogService")
public class SdcCdssItemLogServiceImpl extends BaseServiceImpl<SdcCdssItemLogMapper, SdcCdssItemLogEntity> implements ISdcCdssItemLogService {

    @Resource
    private SdcCdssItemMapper sdcCdssItemMapper;
    @Resource
    private SdsQcentityMapper sdsQcentityMapper;

    @Override
    public List<SdcCdssItemLogEntity> page(Page<SdcCdssItemLogEntity> page, Map<String, Object> map) {
        Integer itemType = null;
        if (!StringUtils.isEmpty(map.get("itemType"))){
            String recommendType = (String) map.get("itemType");
            switch (recommendType){
                case "推荐护理处置":
                    itemType = 5;
                    break;
                case "推荐检验":
                    itemType = 3;
                    break;
                case "推荐检查":
                    itemType = 2;
                    break;
                case "推荐治疗方案":
                    itemType = 4;
                    break;
                case "推荐评估量表":
                    itemType = 1;
                    break;
            }
        }
        map.put("itemType", itemType);
        List<SdcCdssItemLogEntity> page1 = this.baseMapper.page(page, map);
        for (SdcCdssItemLogEntity sdcCdssItemLogEntity : page1){
            SdsQcentityEntity sdsQcentityEntity = sdsQcentityMapper.selectById(sdcCdssItemLogEntity.getEntityId());
            sdcCdssItemLogEntity.setEntityName(sdsQcentityEntity.getBtDesc());
        }
        return page1;
    }

    @Override
    public List<SdcCdssItemLogEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public void deleteLog(List<String> nodeIds) {
        User user = ShiroKit.getUser();
        String userName = user.getLoginName();
        List<SdcCdssItemEntity> sdcCdssItemEntities = sdcCdssItemMapper.selectByNodeIds(nodeIds);
        for (SdcCdssItemEntity sdcCdssItemEntity : sdcCdssItemEntities){
            SdcCdssItemLogEntity sdcCdssItemLogEntity = new SdcCdssItemLogEntity();
            sdcCdssItemLogEntity.setEntityId(sdcCdssItemEntity.getQcentityId());
            sdcCdssItemLogEntity.setItemType(sdcCdssItemEntity.getItemType());
            sdcCdssItemLogEntity.setOperTime(new Date());
            sdcCdssItemLogEntity.setOperContent(sdcCdssItemEntity.getItemName());
            sdcCdssItemLogEntity.setOperType(3);
            sdcCdssItemLogEntity.setOperUser(userName);
            sdcCdssItemLogEntity.insert();
        }
    }
}
